package com.example.zhu95.mytimber.listener;

import android.view.View;

import com.example.zhu95.mytimber.ConstantValue;
import com.example.zhu95.mytimber.MyApplication;
import com.example.zhu95.mytimber.activity.BaseActivity;
import com.example.zhu95.mytimber.entity.PlayQueueRecord;
import com.example.zhu95.mytimber.entity.SongBean;
import com.example.zhu95.mytimber.entity.SongData;
import com.example.zhu95.mytimber.utils.Util;

import java.util.List;

/**
 * @author zhu95 on 2018/3/21.
 */

public class OnAllRandomPlayClickListener implements View.OnClickListener {
    @Override
    public void onClick(View v) {
        PlayQueueRecord record = new PlayQueueRecord();
        record.setPlayMode(ConstantValue.PLAYMODE_RANDOM);
        record.updateAll();

        List<SongBean> playqueueSongs = PlayQueueRecord.Companion.getPlayqueueSongs();
        //如果当前播放列表的歌曲数为1 或 o
        if (playqueueSongs.size() < 2) {
            MyApplication.sendPlayMusicCmdBroadcast(BaseActivity.songid, null, ConstantValue.PlayMusicCmd_Play, -1);
            return;
        }
        int index = playqueueSongs.indexOf(SongData.findSongByID(BaseActivity.songid));
        int num = index;
        while (num == index) {
            num = Util.INSTANCE.getRandomPositiveNum();
            num = num % playqueueSongs.size();
        }
        index = num;
        MyApplication.sendPlayMusicCmdBroadcast(playqueueSongs.get(index).getId(), null, ConstantValue.PlayMusicCmd_Play, 0);
    }
}
